package com.tea.app.moudles.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tea.app.common.util.session.SessionUtil;
import com.tea.app.moudles.mapper.OrderMapper;
import com.tea.app.moudles.service.OrderServiceImpl;
import com.tea.common.common.annotation.AccessLimiter;
import com.tea.common.common.annotation.NeedLogin;
import com.tea.common.common.constant.Const;
import com.tea.common.common.enums.OrderStatus;
import com.tea.common.common.exception.ServiceException;
import com.tea.common.common.util.PayjsUtil;
import com.tea.common.common.util.result.Result;
import com.tea.common.entity.app.OrderInfo;
import com.tea.common.entity.app.form.CreateOrderForm;
import com.tea.common.entity.app.vo.HistoryOrderVO;
import com.tea.common.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * 订单控制层
 */
@Api(tags = "订单服务")
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderServiceImpl orderService;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private RedisService redisService;

    /**
     * 下订单
     * @param orderParams 订单参数
     * @param request 请求
     * @return 订单号
     * @throws ServiceException 服务异常
     */
    @ApiModelProperty("下订单")
    @NeedLogin
    @PostMapping
    public Result<String> createOrder(@RequestBody CreateOrderForm orderParams, HttpServletRequest request) throws ServiceException {
       //用户下订单
        return orderService.createOrder(orderParams, SessionUtil.getCurrentWxOpenidFromRequest(request));
    }

    // 以下支付相关API已废弃，订单创建后直接进入制作中状态
    @Deprecated
    @ApiOperation("获取payJS支付小程序支付需要的支付参数信息(已废弃)")
    @NeedLogin
    @AccessLimiter
    @GetMapping("/payMP/{orderNo}")
    public Result payJSMP(@PathVariable String orderNo, HttpServletRequest request) throws ServiceException {
        return Result.fail("支付功能已移除，订单创建后直接进入制作中状态");
    }

    // 以下支付相关API已废弃，订单创建后直接进入制作中状态
    @Deprecated
    @ApiOperation("h5收银台支付方式(已废弃)")
    @NeedLogin
    @GetMapping("/payCashier/{orderNo}")
    public Result<Integer> cashier(@PathVariable String orderNo, HttpServletRequest request) throws ServiceException, UnsupportedEncodingException {
        return Result.fail("支付功能已移除，订单创建后直接进入制作中状态");
    }

    // 以下支付相关API已废弃，订单创建后直接进入制作中状态
    @Deprecated
    @ApiOperation("完成支付(已废弃)")
    @NeedLogin
    @PutMapping("/finishWeixinPay")
    public Result finishWeixinPay(@RequestBody Map<String, String> data) {
        return Result.fail("支付功能已移除，订单创建后直接进入制作中状态");
    }

    // 以下支付相关API已废弃，订单创建后直接进入制作中状态
    @Deprecated
    @ApiOperation(value = "接收支付返回的消息(已废弃)")
    @AccessLimiter(seconds = 5, maxCount = 30)
    @PostMapping(value = "/pay/notify")
    @Transactional
    public Result parseOrderNotifyResult(HttpServletRequest req, HttpServletResponse response) {
        return Result.fail("支付功能已移除，订单创建后直接进入制作中状态");
    }

    @ApiOperation("取消订单")
    @NeedLogin
    @DeleteMapping("/cancel/{orderNo}")
    public Result<String> cancelOrder(@ApiParam("订单号") @PathVariable String orderNo) {
        return orderService.cancelOrder(orderNo);
    }

    @ApiOperation("确认收货")
    @NeedLogin
    @PutMapping("/confirmReceive/{orderNo}")
    public Result confirm(@ApiParam("订单号") @PathVariable String orderNo, HttpServletRequest request) throws ServiceException {
        return Result.ok(orderService.finishedOrder(orderNo, SessionUtil.getCurrentWxOpenidFromRequest(request)));
    }

    @ApiOperation("分页获取历史订单")
    @NeedLogin
    @GetMapping("/history/page/{pageNo}/{pageSize}")
    public Result<Page<HistoryOrderVO>> getHistoryOrderByPage(@PathVariable Integer pageNo,
                                                              @PathVariable Integer pageSize,
                                                              HttpServletRequest request) throws ServiceException {
        return Result.ok(orderService.getHistoryOrderByPage(pageNo, pageSize, SessionUtil.getCurrentWxOpenidFromRequest(request)));
    }

    @ApiOperation("订单详情信息")
    @GetMapping("/detail/{orderNo}")
    public Result<OrderInfo> getOrderDetail(@PathVariable String orderNo) throws ServiceException {
        return Result.ok(orderService.getOrderDetail(orderNo));
    }

    @ApiOperation("获取正在处理的订单列表")
    @NeedLogin
    @AccessLimiter
    @GetMapping("/notCompleted")
    public Result<List<OrderInfo>> getHandlingOrders(HttpServletRequest request) throws ServiceException {
        return Result.ok(orderService.getHandlingOrders(SessionUtil.getCurrentWxOpenidFromRequest(request)));
    }

    @ApiOperation("根据门店ID获取订单列表")
    @GetMapping("/store/{storeId}")
    public Result<List<OrderInfo>> getOrdersByStoreId(@PathVariable Long storeId) {
        return Result.ok(orderMapper.getOrdersByStoreId(storeId));
    }

}
